<?php

namespace App\Admin\Controllers;

use App\Models\Shop;
use App\Models\ShopClerk;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;

class ClerkController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(ShopClerk::class, function (Grid $grid) {
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->like('shop.shop_name', '商铺名');
                $filter->like('name', '名字');
                $filter->like('mobile', '手机号');
            });
            $grid->id('ID')->sortable();
            $grid->column('shop.shop_name', '商铺名');
            $grid->column('name', '名字');
            $grid->column('mobile', '手机号');
            //$grid->column('password', '密码');
            $grid->role('职位')->display(function ($role){
                switch ($role){
                    case '0':
                        return '财务';
                        break;
                    case '1':
                        return '服务层';
                        break;
                    case '2':
                        return '经理';
                        break;
                    case '3':
                        return '主管';
                        break;
                }
            });
            $grid->column('avatar', '头像');
            $grid->sex('性别')->display(function ($sex){
                switch ($sex){
                    case '0':
                        return '女';
                        break;
                    case '1':
                        return '男';
                        break;
                }
            });
            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(ShopClerk::class, function (Form $form) {
            $form->saving(function (Form $form) {
                $shop = Shop::find($form->shop_id);
                if($shop == null){
                    $error  = new MessageBag([
                        'title'   => '店铺不存在',
                    ]);
                    return back()->with(compact('error'));
                }
            });

            $form->text('shop_id', '隶属商家id');
            $form->text('name', '名字');
            $form->text('mobile', '手机号');
            $form->text('password', '密码');
            $form->radio('role','职位')->options(['0' => '财务', '1'=> '服务员',2 => '经理',3 => '主管']);
            $form->image('avatar', '头像');
            /*$form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');*/
        });
    }
}
